package com.longlonggo.action;

import com.alibaba.fastjson.JSON;
import com.longlonggo.constant.Constant;
import com.longlonggo.constant.ConstantKey;
import com.longlonggo.entity.ArticleEntity;
import com.longlonggo.entity.CategoryEntity;
import com.longlonggo.entity.SysDictEntity;
import com.longlonggo.service.ArticleService;
import com.longlonggo.service.CategoryService;
import com.longlonggo.service.SysDictService;
import com.longlonggo.vo.RestVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文章表页面控制层
 *
 * @author 石马人山 me@longlonggo.com
 * @version 100-000-000<br/>
 * @Created 2017年8月9日 上午11:17:35
 * </p>
 * @description <br/>
 */
@Slf4j
@Controller
@RequestMapping("/web/article")
public class ArticleAction extends BaseAction {

    /**
     * 文章业务层
     */
    @Autowired
    ArticleService articleService;
    /**
     * 栏目业务层
     */
    @Autowired
    CategoryService categoryService;

    /**
     * 字典类
     */
    @Autowired
    SysDictService sysDictService;

    public ArticleAction() {
        System.out.println("-----------start");
    }

    /**
     * @Description:文章主页
     * @author：刘跃卫
     * @Date: 2018/12/4 9:45
     */
    @RequestMapping(value = "/index")
    public String index(HttpServletRequest request, Model model) {
        ArticleEntity article = new ArticleEntity();

        System.out.println("-------------find");
        model.addAttribute("myName", "yyyy");
        return "/article/index";
    }


    /**
     * 添加文章内容页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/add")
    public String add(HttpServletRequest request, Model model) {
        ArticleEntity article = new ArticleEntity();
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("categoryType", Constant.CATEGORY_TYPE_0);
        List<CategoryEntity> categoryEntityList = (List<CategoryEntity>) categoryService.queryListByMap(map);

        List<SysDictEntity> sysDictEntityList = (List<SysDictEntity>) sysDictService.queryListByCode(ConstantKey.ARTICLE_STATUS);
        model.addAttribute("categoryEntityList", categoryEntityList);
        model.addAttribute("sysDictEntityList", sysDictEntityList);

        return "/article/add";
    }

    /**
     * 添加文章内容页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    public String saveOrUpdate(HttpServletRequest request, @RequestBody ArticleEntity article) {
        logger.info(JSON.toJSONString(article));
        if (article.getArticleId() == null || article.getArticleId() == 0) {
            articleService.insert(article);
        }
        return "success";
    }

    @RequestMapping(value = "/queryListByMap")
    @ResponseBody
    public RestVo queryListByMap(HttpServletRequest request, @RequestBody Map<String, Object> params) {
//        Map<String, Object> params = new HashMap<>();
        logger.info(JSON.toJSONString(params));
        return RestVo.SUCCESS(articleService.queryListByMap(params));
    }
}
